package com.base.cn.platform.os.controller.classes.member;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.manage.op.affiche.OpReceiveAfficheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 班级成员
 *
 * @author SK
 * @since 2018-05-10
 */
@Controller
@RequestMapping("/admin/classes/member")
public class AdminClassesMemberController extends AdminBaseController {

    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private OpReceiveAfficheService opReceiveAfficheService;

    /**
     * 班级成员
     *
     * @param member      班级成员查询条件
     * @param currentPage 当前页
     * @return 班级成员列表
     */
    @RequestMapping("/ajaxClassesMemberPage")
    public String ajaxClassesMemberPage(Model model,
                                        @RequestBody Map<String, Object> member,
                                        @RequestParam("currentPage") Integer currentPage) {
        Map<String, Object> dataMap = classesMemberService.findClassesMemberPage(member, currentPage, null, true, 0, 1, 2);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/member/ajax-classes-member-list");
    }

    /**
     * 保存班级成员
     *
     * @param classesId 班级id
     * @param memberIds 成员id
     * @param identity  1.普通学员 2.班级管理员 3.班主任
     * @return 保存结果
     */
    @RequestMapping("/saveMemberBatch")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加班级成员")
    public Map<String, Object> saveMemberBatch(@RequestParam("classesId") BigDecimal classesId,
                                               @RequestParam("memberIds") String memberIds,
                                               @RequestParam("identity") Integer identity) {
        return classesMemberService.saveClassesMemberBatch(classesId, memberIds, identity, 2);
    }

    /**
     * 发送班级学员通知消息
     *
     * @param params 消息
     * @return 发送结果
     */
    @RequestMapping("/sendMemberNotice")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "发送班级学员通知消息")
    public Map<String, Object> sendMemberNotice(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        params.put("msgType", 1);
        params.put("status", 1);
        BigDecimal loginUserId = loginUtil.getLoginUserId(request);
        params.put("createUserId",loginUserId);
        return opReceiveAfficheService.saveReceiveAffiche(params);
    }

    /**
     * 更新班级成员状态
     *
     * @param ids    班级成员id
     * @param status 1.正常 2.封禁
     * @return 操作结果
     */
    @RequestMapping("/updateMemberStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级成员状态")
    public Map<String, Object> updateMemberStatus(@RequestParam("ids") String ids,
                                                  @RequestParam("status") Integer status) {
        return classesMemberService.updateClassesMemberStatus(ids, status);
    }

    /**
     * 更新班级成员身份
     *
     * @param ids      班级成员id
     * @param identity 1.普通学员 2.班级管理员 3.班主任
     * @return 操作结果
     */
    @RequestMapping("/updateMemberIdentity")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级成员身份")
    public Map<String, Object> updateMemberIdentity(@RequestParam("ids") String ids,
                                                    @RequestParam("identity") Integer identity) {
        return classesMemberService.updateClassesMemberIdentity(ids, identity);
    }

    /**
     * 查询班级通知
     * @param model
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxClassesLetterPage")
    public String ajaxClassesLetterPage(Model model,
                                        @RequestParam("currentPage") Integer currentPage,
                                        @RequestBody Map<String, Object> params){
        params.put("msgType",1);
        Map<String, Object> afficheMap = opReceiveAfficheService.queryAjaxReceiveAfficheLetterList(currentPage, params, true, true, true);
        model.addAttribute("dataMap",afficheMap);
        return resultPage("/classes/member/ajax-classes-letter-list");
    }

    /**
     * 删除班级通知
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteReceiveAfficheIds")
    @ResponseBody
    public Map<String, Object> deleteReceiveAfficheIds(@RequestParam("ids")String ids,@RequestParam(value = "userId",defaultValue = "0",required = false)BigDecimal userId){
        Map<String, Object> response = opReceiveAfficheService.deleteReceiveAfficheIds(ids,userId);
        return response;
    }

    /**
     * 查看班级通知详情
     * @param model
     * @return
     */
    @RequestMapping("/letterInfo")
    public String letterInfo (Model model,
                              @RequestParam("afficheRandom") String afficheRandom,
                              @RequestParam("classId") BigDecimal classId){
        model.addAttribute("afficheRandom",afficheRandom);
        model.addAttribute("classId",classId);
        return resultPage("/classes/member/letter-info");
    }

    /**
     * ajax加载查看班级通知列表
     * @param model
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/ajaxClassesLetterInfo")
    public String ajaxClassesLetterInfo(Model model,
                                        @RequestParam("currentPage")Integer currentPage,
                                        @RequestBody Map<String,Object> params){
        params.put("msgType",1);
        Map<String, Object> afficheMap = opReceiveAfficheService.queryAjaxReceiveAfficheList(currentPage, params, false, false, false);
        model.addAttribute("dataMap",afficheMap);
        return resultPage("/classes/member/ajax-letter-info");
    }

    /**
     * 删除班主任
     *
     * @param id      班级成员id
     * @return 操作结果
     */
    @RequestMapping("/deleteClassTeacher")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除班主任")
    public Map<String, Object> deleteClassTeacher(@RequestParam("id") BigDecimal id) {
        return classesMemberService.deleteClassTeacher(id);
    }

    /**
     * 删除班级消息
     *
     * @param afficheRandoms
     * @return 操作结果
     */
    @RequestMapping("/deleteReceiveClassAfficheIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除班级消息")
    public Map<String, Object> deleteReceiveClassAfficheIds(@RequestParam("afficheRandoms") String afficheRandoms) {
        return opReceiveAfficheService.deleteReceiveClassAfficheIds(afficheRandoms);
    }
}
